const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(bodyParser.json());
app.use(express.static('public'));

// 固定账号密码
const ADMIN_CREDENTIALS = {
    username: 'admin',
    password: '123456'
};

// 登录接口
app.post('/login', (req, res) => {
    const { username, password } = req.body;
    
    if (username === ADMIN_CREDENTIALS.username && password === ADMIN_CREDENTIALS.password) {
        res.json({ success: true });
    } else {
        res.json({ success: false });
    }
});

// 欢迎页面重定向
app.get('/welcome', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'welcome.html'));
});

// 退出登录
app.post('/logout', (req, res) => {
    res.json({ success: true });
});

// 根路径重定向到登录页
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
    console.log('默认账号: admin');
    console.log('默认密码: 123456');
});